(() => {

'use strict';
goog.require('Blockly.Blocks');
goog.require('Mixly.Boards');

const { Boards } = Mixly;
const BOARDS_TYPE = Boards.getType();
const QMC5883L_HUE = 40;
const SVG_PATH = "./libraries/ThirdParty/QMC5883L/media/compass.svg";

switch (BOARDS_TYPE) {
    case 'arduino_avr':
        // 初始化QMC5883L电子罗盘模块
        Blockly.Blocks.qmc5883l_init = {
            init: function() {
                this.appendDummyInput()
                    .appendField(new Blockly.FieldImage(SVG_PATH, 20, 20, "*"))
                    .appendField(Blockly.Msg.QMC5883L_INIT)
                    .appendField(new Blockly.FieldTextInput("compass"), "NAME");
                this.setInputsInline(true);
                this.setPreviousStatement(true, null);
                this.setNextStatement(true, null);
                this.setColour(QMC5883L_HUE);
                this.setTooltip("");
                this.setHelpUrl("");
                this.updateShape('H_I2C');
            },
            mutationToDom: function () {
                let container = Blockly.utils.xml.createElement('mutation');
                let type = this.getFieldValue('TYPE');
                container.setAttribute('i2c_type', type);
                return container;
            },
            domToMutation: function (xmlElement) {
                let type = xmlElement.getAttribute('i2c_type');
                this.updateShape(type);
            },
            updateShape: function (type) {
                for (let id of ['SDA', 'SCL', 'DUMMY_TYPE']) {
                    if (this.getInput(id)) {
                        this.getInputTargetBlock(id)?.dispose();
                        this.removeInput(id);
                    }
                }
                this.appendDummyInput("DUMMY_TYPE")
                    .appendField(Blockly.Msg.QMC5883L_USE)
                    .appendField(new Blockly.FieldDropdown([
                        ["Wire", "H_I2C"],
                        ["SoftwareWire", "S_I2C"]
                    ], (value) => {
                        if (value !== type) {
                            this.updateShape(value);
                            this.setFieldValue(value, "TYPE");
                            return null;
                        }
                        return undefined;

                    }), "TYPE");
                if (type === 'S_I2C') {
                    let sdaObj = this.appendValueInput("SDA");
                    sdaObj.setCheck(null);
                    sdaObj.appendField("SDA#");
                    let sclObj = this.appendValueInput("SCL");
                    sclObj.setCheck(null);
                    sclObj.appendField("SCL#");
                    this.moveInputBefore("SCL", "DUMMY_TYPE");
                    this.moveInputBefore("SDA", "SCL");
                    let sdaBlock = Blockly.mainWorkspace.newBlock('pins_digital');
                    sdaBlock.setFieldValue(profile.default.digital[0][1], 'PIN');
                    sdaBlock.initSvg();
                    sdaBlock.render();
                    let sdaConnection = sdaObj.connection;
                    sdaConnection.connect(sdaBlock.outputConnection);
                    sdaBlock.setShadow(true);
                    let sclBlock = Blockly.mainWorkspace.newBlock('pins_digital');
                    sclBlock.setFieldValue(profile.default.digital[1][1], 'PIN');
                    sclBlock.initSvg();
                    sclBlock.render();
                    let sclConnection = sclObj.connection;
                    sclConnection.connect(sclBlock.outputConnection);
                    sclBlock.setShadow(true);
                }
            }
        };
        break;
    default:
        Blockly.Blocks.qmc5883l_init = {
            init: function() {
                this.appendDummyInput()
                    .appendField(new Blockly.FieldImage(SVG_PATH, 20, 20, "*"))
                    .appendField(Blockly.Msg.QMC5883L_INIT)
                    .appendField(new Blockly.FieldTextInput("compass"), "NAME");
                this.appendValueInput('SDA')
                    .setCheck(null)
                    .appendField('SDA#');
                this.appendValueInput('SCL')
                    .setCheck(null)
                    .appendField('SCL#');
                this.setInputsInline(true);
                this.setPreviousStatement(true, null);
                this.setNextStatement(true, null);
                this.setColour(QMC5883L_HUE);
                this.setTooltip("");
                this.setHelpUrl("");
            }
        };
}

// QMC5883L电子罗盘模块刷新数据
Blockly.Blocks.qmc5883l_read = {
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage(SVG_PATH, 20, 20, "*"))
            .appendField("QMC5883L")
            .appendField(new Blockly.FieldTextInput("compass"), "NAME")
            .appendField(Blockly.Msg.QMC5883L_REFRESH);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(QMC5883L_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

// QMC5883L电子罗盘模块获取方位角
Blockly.Blocks.qmc5883l_get_azimuth = {
    init: function() {
        this.appendDummyInput()
            .appendField(new Blockly.FieldImage(SVG_PATH, 20, 20, "*"))
            .appendField("QMC5883L")
            .appendField(new Blockly.FieldTextInput("compass"), "NAME");
        this.appendDummyInput()
            .appendField(Blockly.Msg.QMC5883L_GET_AZIMUTH);
        this.setOutput(true, null);
        this.setInputsInline(true);
        this.setColour(QMC5883L_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

})();
